<?php

require_once("../models/config.php");
require_once("../ui.php");
include_once("plot.graph.php");

if(!isUserLoggedIn()) {
	header ("location: login.php");
	die();
}

global $loggedInUser;
global $db;

function createSQLCommand() {

	$sql = "SELECT `root_Cause_Complexities`.`Name` AS ComplexityName, `debugging_Reports`.`Debugging_Time` AS DebuggingTime 
		FROM `debugging_Reports` JOIN `userCake_Users` ON `User` = `userCake_Users`.`User_ID` JOIN `root_Cause_Complexities` ON `Root_Cause_Complexity` = `root_Cause_Complexities`.`Id`
		WHERE (`userCake_Users`.`Group_Id` = " . STANDART_USER_GROUP_ID . ")";
		
	return $sql;
}

$sql = createSQLCommand();
$result = $db->sql_query($sql);
if (!$result) {
	$error = $db->sql_error();
	return;
}

$rows = $db->sql_fetchall();

$complexityNames = array();
$complexityIndexes = array();
$debuggingTimes = array();

foreach($rows as $i => $row) {
	$debuggingTime = round($row["DebuggingTime"] / 60, 2);
	$complexityName =  $row["ComplexityName"];
	
	if (!in_array($complexityName, $complexityNames)) {
		$complexityNames[] = $complexityName;
	}
	
	$complexityIndex = array_search($complexityName, $complexityNames);
	
	$complexityIndexes[] = $complexityIndex;
	$debuggingTimes[] = $debuggingTime; 
}

drawMyPlotGraph($debuggingTimes, $complexityIndexes, $complexityNames, "Complexities of debugged problems",
		"Debugging time [hours]", "How debugging time depends on complexity of debugged issues",
		array("DrawMedians" => TRUE));

